//
//  SecondViewController.m
//  UI_豆瓣app
//
//  Created by 151129-49宋强 on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "SecondViewController.h"
#import "MovieCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "JiuViewController.h"

@interface SecondViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)JiuViewController *jiuVC;
@property (nonatomic, assign) BOOL isT;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightBtn)];
    
    
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    
    [self.view addSubview:_tableView];
    //注册
    [_tableView registerClass:[MovieCell class] forCellReuseIdentifier:@"cell"];
    //数据
    [self dataHandler];
    
    self.jiuVC = [[JiuViewController alloc] init];
    self.isT = NO;
    
}
- (void)rightBtn
{
   // JiuViewController *jiuVC = [[JiuViewController alloc] init];
    _isT = !_isT;
    if (_isT) {
        
   
    [UIView transitionFromView:self.view toView:_jiuVC.view duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        
    }];

    }else{
        [UIView transitionFromView:_jiuVC.view toView:self.view duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            
        }];

    }
}


//数据处理的目的:数组+字典->数组加对象

- (void)dataHandler
{
    NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"]];
    
    self.dataArr = [MovieModel modelHandlerWithArray:arr];
    

   // self.dataArr = [NSMutableArray array];
//    for (NSDictionary *dic in arr) {
//        MovieModel *m = [MovieModel modelWithDic:dic];
//        
//       // MovieModel *m = [[MovieModel alloc] initWithDic:dic];
////       MovieModel *m = [[MovieModel alloc] init];
////        [m setValuesForKeysWithDictionary:dic];
//        m.largeImage = dic[@"images"][@"large"];
//        [_dataArr addObject:m];
//    }
    
}
#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MovieModel *m = _dataArr[indexPath.row];
    //使用SDWebImageView第三方  加载网络图片
    //string ->url ->添加到imageView上
    [cell.movieImg sd_setImageWithURL:[NSURL URLWithString:m.largeImage]];
    cell.moveTitle.text = m.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
